package com.plugin.manager.utll;

import com.plugin.common.LoginService;
import com.plugin.common.UserVo;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LoginFactory {

    public static final String PLUGIN_PATH = "D:\\plugins";
    public static final Map<String, LoginService> map = new ConcurrentHashMap<>();

    public static LoginService getService(String tenantId) throws MalformedURLException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        if (map.containsKey(tenantId)) {
            return map.get(tenantId);
        } else {
            Plugin plugin = new Plugin();
            plugin.setJar(String.format("%s\\login-%s.jar", PLUGIN_PATH, tenantId));
            plugin.setClassName(String.format("com.plugin.test.%s.LoginServiceImpl",tenantId));
            PluginManager pluginManager = new PluginManager(plugin);
            LoginService loginService = pluginManager.getLoginService(plugin.getClassName());
            if (loginService != null) {
                map.put(tenantId, loginService);
            }
        }
        return map.get(tenantId);
    }


}
